<template>
  <el-container class="home-container">
    <el-header><!-- 头部区 -->
      <div>
        <span id="fontLogo">智能快递柜后台管理系统</span>
      </div>
      <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          id="indexMenu">
        <el-menu-item index="1" @click="shouye">首页</el-menu-item>
        <el-menu-item index="2">会员</el-menu-item>
        <el-menu-item index="3" @click="goToMachine">柜机</el-menu-item>
        <el-menu-item index="4" @click="baoguo">包裹</el-menu-item>
        <el-menu-item index="5" @click="tiaozhuan">财务</el-menu-item>
        <el-menu-item index="6" @click="other">其他</el-menu-item>
      </el-menu>
      <div>
        <span id="fontName">{{this.name}}</span>
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="210px" hight="100%">
        <el-menu
            :default-active="activeIndex1"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">

          <el-menu-item index="1">
            <i class="el-icon-menu"></i>
            <span>快递员列表</span>
          </el-menu-item>
          <el-menu-item index="2">
            <i class="el-icon-menu"></i>
            <span slot="title" @click="expressAuth">快递员认证审核</span>
          </el-menu-item>
          <el-menu-item index="3">
            <i class="el-icon-setting"></i>
            <span slot="title" @click="expressMember">普通会员列表</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                size="mini"
                @click="handleAdd"
            >新增快递员</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="this.expressListList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="快递员ID" align="center" prop="id">
            <template slot-scope="scope">
              <span @click="detialById(scope.row.id)"  style="color: #1890ff;">{{scope.row.id}}</span>
            </template>
          </el-table-column>
          <el-table-column label="注册类型" align="center" prop="registerType" >
            <template slot-scope="scope">
              <span v-if="scope.row.registerType==1">自主注册</span>
              <span v-if="scope.row.registerType==2">后台创建</span>
            </template>
          </el-table-column>
          <el-table-column label="手机号" align="center" prop="phone">
          <template slot-scope="scope">
            <span @click="detialByPhone(scope.row.phone)">{{scope.row.areaCode+' '+scope.row.phone}}</span>
          </template>
          </el-table-column>
          <el-table-column label="真实姓名" align="center" prop="name" />

          <el-table-column label="快递公司" align="center" prop="postCompany" />
          <el-table-column label="创建时间" align="center" prop="createDate" width="180">
            <template slot-scope="scope">
              {{scope.row.createDate|format('yyyy-MM-dd hh:mm:ss')}}
            </template>
          </el-table-column>
          <el-table-column label="注册IP" align="center" prop="registerIp" />
          <el-table-column label="会员余额" align="center" prop="accountBalance" />
          <el-table-column label="实名认证" align="center" prop="realNameStatus" >
          <template slot-scope="scope">
            <span v-if="scope.row.realNameStatus==1">已认证</span>
            <span v-if="scope.row.realNameStatus==2">未认证</span>
            <span v-if="scope.row.realNameStatus==3">已拒绝</span>
          </template>
          </el-table-column>
          <el-table-column label="会员状态" align="center" prop="status" >
          <template slot-scope="scope">
            <span v-if="scope.row.status==1">正常</span>
            <span v-if="scope.row.status==2">禁用</span>
          </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="resetPassword(scope.row.id)"
              >重置登录密码</el-button>
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="sernullPayPassword(scope.row.id)"
                >重置支付密码</el-button>
                <el-button
                    size="mini"
                    type="text"
                    @click="updateStatusFalse(scope.row.registerIp)"
                    v-if="scope.row.status==1"
                >禁用</el-button>
                <el-button
                    size="mini"
                    type="text"
                    @click="updateStatusTrue(scope.row.registerIp)"
                    v-if="scope.row.status==2"
                >启用</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNumber"
              :page-sizes="[ 4, 6, 8]"
              :page-size=pageSize
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>

        <!-- 添加或修改快递员列表对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="区号" prop="areaCode">
              <el-select v-model="form.areaCode" placeholder="请选择区号">
                <el-option label="+86 中国" value="+86"></el-option>
                <el-option label="+63 菲律宾" value="+63"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="快递员手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" pattern="[0-9]{1,11}" oninput="this.value=this.value.slice(0,this.maxLength)" maxLength="11"/>
            </el-form-item>
            <el-form-item label="真实姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入快递员姓名" />
            </el-form-item>
            <el-form-item label="快递公司" prop="postCompany">
              <el-select v-model="form.postCompany" placeholder="请选择快递公司">
                <el-option label="小莫配送" value="小莫配送"></el-option>
                <el-option label="菲度" value="菲度"></el-option>
                <el-option label="FoodPanda" value="FoodPanda"></el-option>
                <el-option label="HonestBee" value="HonestBee"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import {listExpressList} from "@/api/expressList/expressList";
import axios from "axios";
export default {
  data() {
    return {
      pageNumber: 1,
      pageSize: 4,
      total: 0,
      // idd:'',
      // phone:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      // 快递员列表表格数据
      expressListList: [{

      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        registrationType: null,
        createDate: null,
        postCompany: null,
        registerIp: null,
        balance: null,
        verification: null,
        status: null,
        phone: null,
        areaCode: null,
        photo: null,
        changeDate: null,
        isDel: null,
        password: null,
        payPassword: null,
        account: null,
        email: null
      },
      // 表单参数
      form: {
        id:'',
      },
      // 表单校验
      rules: {
        isDel: [
          { required: true, message: "逻辑删除 1否 2是  不能为空", trigger: "blur" }
        ],
      },
      activeIndex: '2',
      activeIndex1:'1',
      name: sessionStorage.getItem("name"),
    };
  },
  created() {
    this.getList();
    if (this.name==null){
      this.name="未登录";
      // this.$router.push("/login");
    }
    //分页
    axios.get('http://localhost:10020/expressList/list?pageNum='+this.pageNumber+'&pageSize='+this.pageSize).then(response=>{
      this.expressListList=response.data.list;
      this.total=response.data.total;
    });
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize=val;
      axios.get('http://localhost:10020/expressList/list?pageNum='+this.pageNumber+'&pageSize='+this.pageSize).then(response=>{
        this.expressListList=response.data.list;
        this.total=response.data.total;
      });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber=val;
      axios.get('http://localhost:10020/expressList/list?pageNum='+this.pageNumber+'&pageSize='+this.pageSize).then(response=>{
        this.expressListList=response.data.list;
        this.total=response.data.total;
      });
    },
    getList() {
      this.loading=true;
      // 异步更新数据
      axios.get('http://localhost:10020/expressList/list?pageNum='+this.pageNumber+'&pageSize='+this.pageSize).then(response=>{
        this.expressListList = response.data.list;
        // 加载状态结束
        this.loading = false;
      })

    },
    // 跳转详情页面
    detialById(id){
      this.$router.push(
          {path: "/expressDetial",
            query: {
              id: id,
            },
          })
    },
    // 跳转详情页面
    detialByPhone(phone){
      this.$router.push(
          {path: "/expressDetial",
            query: {
              phone: phone,
            },
          })
    },
    //添加确定按钮
    submitForm(){
      axios.post("http://localhost:10020/expressList/expressAdd",this.form).then(response =>{
        this.form = response.data;
        this.$message("新增成功");
        this.open = false;
        this.getList();
      })
    },
    // 状态变更
    updateStatusFalse(id){
      axios.put("http://localhost:10020/expressList/updateStatusFalse?id="+id).then(response =>{
        this.form = response.data;
        this.getList();

      })
    },
    // 状态变更
    updateStatusTrue(id){
      axios.put("http://localhost:10020/expressList/updateStatusTrue?id="+id).then(response =>{
        this.form = response.data;
        this.getList();
      });
    },
    // 置空支付密码
    sernullPayPassword(id){
      this.$confirm('此操作将置空该密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.put("http://localhost:10020/expressList/sernullPayPassword?id="+id).then(response =>{
          this.form = response.data;
          this.getList();
        });
        this.$message({
          type: 'success',
          message: '已置空，请前往设置支付密码'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消置空'
        });
      });

    },
    // 密码重置为123456
    resetPassword(id){
      this.$confirm('此操作将重置该密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.put("http://localhost:10020/expressList/resetPassword?id="+id).then(response =>{
          this.form = response.data;
          this.getList();
        });
        this.$message({
          type: 'success',
          message: '重置成功!默认密码为123456'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        });
      });

    },
    expressDetial(){
      this.$router.push(
          {path: "/expressDetial"}
      )
    },
    xitong(){
      this.$router.push(
          {path: "/OutSystems"}
      )
    },
    baoguo(){
      this.$router.push(
          {path: "/OutPackage"}
      )
    },
    notice() {
      this.$router.push(
          {path: "/HomePage"}
      )
    },
    //跳转其他页面
    other() {
      this.$router.push(
          {path: "/otherView"}
      )
    },
    tiaozhuan(){
      this.$router.push({
        path:'/zhanghuPage'
      })
    },
    goToMachine(){
      this.$router.push(
          {path: "/machineHome"}
      )
    },
    shouye(){
      this.$router.push(
          {path: "/"}
      )
    },
    handleSelect(key) {
      console.log(key);
    },

    // 页面跳转
    expressMember(){
      this.$router.push(
          {path: "/expressMember"}
      )
    },
    // 页面跳转
    expressAuth(){
      this.$router.push(
          {path: "/expressAuth"}
      )
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        postCompany: null,
        phone: null,
        areaCode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.open = true;
      this.title = "添加快递员列表";
    },
  },
  filters:{
    format(value,arg){
      function dateFormat(date, format) {
        if (typeof date === "string") {
          var mts = date.match(/(\/Date\((\d+)\)\/)/);
          if (mts && mts.length >= 3) {
            date = parseInt(mts[2]);
          }
        }
        date = new Date(date);
        if (!date || date.toUTCString() == "Invalid Date") {
          return "";
        }
        var map = {
          "M": date.getMonth() + 1, //月份
          "d": date.getDate(), //日
          "h": date.getHours(), //小时
          "m": date.getMinutes(), //分
          "s": date.getSeconds(), //秒
          "q": Math.floor((date.getMonth() + 3) / 3), //季度
          "S": date.getMilliseconds() //毫秒
        };
        format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
          var v = map[t];
          if (v !== undefined) {
            if (all.length > 1) {
              v = '0' + v;
              v = v.substr(v.length - 2);
            }
            return v;
          } else if (t === 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
          }
          return all;
        });
        return format;
      }

      return dateFormat(value,arg);

    }
  },
};
</script>

<style lang="less">
#app {
  margin-top: 0;
}

.home-container {
  height: 100%;
}

.el-header {
  background-color: #545c64;
  display: flex; //设置显示为flex布局
  justify-content: space-between; //设置为flex左右布局
  padding-left: 0; //左内边距为0（Logo贴左边）
  align-items: center; //元素上下居中（防止右边按钮贴上下边）
  color: #fff;
  font-size: 18px;

  > div {
    //内嵌的div样式
    display: flex;
    align-items: center; //Logo和文字上下居中
    #fontLogo {
      width: 200px;
      margin-left: 10px; //文字左侧设置间距，防止与Logo紧贴
    }
    #fontName {
      width: 120px;
      margin-right: 10px;
    }
  }
}

#indexMenu {
  width: 100%;
}

.el-menu {
  background-color: #545c64;
}

.el-aside {
  background-color: #545c64;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;
}

.el-main {
  background-color: #eaedf1;
  height: 1000px;
}
</style>

